from pkg.voice import out
from pkg.driver import get_input,show_content
from pkg.db import db
import gl
import os
import sqlite3
import win32api
import win32con


def default_act(txt):
    print('没有听清楚：'+txt)
    out.output('不好意思，我没有听清楚')


def create_table(table_name):
    out.output_thread('请确认或者修改数据表名')
    table_name = get_input.get_line_input(table_name)
    if len(table_name) == 0:
        out.output_thread('取消建立数据库表')
        return
    out.output_thread('请输入数据库各字段及类型,使用逗号隔开')
    table_content=get_input.get_text_input('')
    if len(table_content)==0:
        out.output_thread('取消建立数据库表')
        return
    if db.exec('CREATE TABLE '+table_name+' ('+table_content+')'):
        out.output_thread('数据库表创建成功')
    else:
        out.output_thread('数据库表创建失败')


def exit_system():
    out.output('正在退出系统，再见')
    os._exit(0)


def exec_sql(sql):
    out.output_thread('请输入要执行的数据库语句')
    sql = get_input.get_text_input(sql)
    if len(sql)==0:
        out.output_thread('取消执行')
    else:
        content,result=db.select_exec(sql)
        if result:
            if len(content)==0:
                out.output_thread('执行成功，但无结果返回')
            else:
                out.output_thread('执行成功，这是执行结果')
                show_content.show_content(content)
        else:
            out.output_thread('执行失败')


def insert_sql(table_name):
    out.output_thread('请确认或者修改数据表名')
    table_name = get_input.get_line_input(table_name)
    if len(table_name) == 0:
        out.output_thread('取消插入')
        return
    out.output_thread('请输入数据库各字段的值，多条数据换行隔开')
    table_content = get_input.get_text_input('')
    if len(table_content) == 0:
        out.output_thread('取消插入')
        return
    data=table_content.split('\n')
    content=''
    for i in data:
        if len(i)!=0:
            content+=' select '+i+' union all '
    content=content[:-11]
    if db.exec('INSERT INTO ' + table_name + content):
        out.output_thread('数据插入成功')
    else:
        out.output_thread('数据插入失败')


def select_all(table_name):
    out.output_thread('请确认或者修改数据表名')
    table_name = get_input.get_line_input(table_name)
    if len(table_name) == 0:
        out.output_thread('取消查看')
        return
    content, result = db.select_exec('SELECT * FROM '+table_name)
    if result:
        if len(content) == 0:
            out.output_thread('执行成功，但无结果返回')
        else:
            out.output_thread('执行成功，这是执行结果')
            show_content.show_content(content)
    else:
        out.output_thread('执行失败')


def music():
    conn, cur = db.get_conn()
    ret=cur.execute('SELECT attribute_value FROM '+gl.g_curr_user[0]+' WHERE attribute_name="MUSIC_EXEC_PATH"')
    ret=ret.fetchall()
    if len(ret)==0:
        out.output_thread('没有设置默认的音乐播放程序，请指定')
        music_exec_name=get_input.get_line_input('')
        if len(music_exec_name)==0:
            out.output_thread('取消播放音乐')
            return

        cur.execute('INSERT INTO '+gl.g_curr_user[0]+' VALUES(?,?)',
                             ("MUSIC_EXEC_PATH",sqlite3.Binary(music_exec_name.encode())))
        conn.commit()
        ret=music_exec_name
    else:
        ret=ret[0][0].decode()
    if not bool(os.popen(ret)):
        out.output_thread('应用程序启动失败')


def play():
    win32api.keybd_event(win32con.VK_MEDIA_NEXT_TRACK, 0, 0, 0)
    win32api.keybd_event(win32con.VK_MEDIA_NEXT_TRACK, 0, win32con.KEYEVENTF_KEYUP, 0)